package com.lostreseditores.sabersai.dataaccess.dao;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

import com.lostreseditores.sabersai.dataaccess.sessionFactory.HibernateSessionFactory;
import com.lostreseditores.sabersai.model.Colegio;
import com.lostreseditores.sabersai.model.dto.ColegioDTO;


public class ColegioDAO implements IColegioDAO {
	private static final Log log = LogFactory.getLog(ColegioDAO.class);

	// property constants
	// public static final Long COLECODDANEN = "colecoddanen";
	public static final String COLECODDANEN = "colecoddanen";

	// public static final String COLEDESCRIPV = "coledescripv";
	public static final String COLEDESCRIPV = "coledescripv";

	// public static final String COLEDIRECCIONV = "coledireccionv";
	public static final String COLEDIRECCIONV = "coledireccionv";

	// public static final String COLEEMAILV = "coleemailv";
	public static final String COLEEMAILV = "coleemailv";

	// public static final String COLEFAXV = "colefaxv";
	public static final String COLEFAXV = "colefaxv";

	// public static final Integer COLEIDN = "coleidn";
	public static final String COLEIDN = "coleidn";

	// public static final String COLELOGOV = "colelogov";
	public static final String COLELOGOV = "colelogov";

	// public static final String COLESECTORV = "colesectorv";
	public static final String COLESECTORV = "colesectorv";

	// public static final String COLETELEFONOFV = "coletelefonofv";
	public static final String COLETELEFONOFV = "coletelefonofv";

	// public static final String COLEZONAV = "colezonav";
	public static final String COLEZONAV = "colezonav";

	// public static final Long LUGAIDN = "lugaidn";
	public static final String LUGAIDN = "lugaidn";

	private Session getSession() {
		return HibernateSessionFactory.getSession();
	}

	/**
	 * 
	 * @param Instance
	 *            Colegio Instance to persist
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void save(Colegio instance) {
		log.debug("saving Colegio instance");

		try {
			getSession().save(instance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/**
	 * @param Instance
	 *            Colegio Instance to delete
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void delete(Colegio instance) {
		log.debug("deleting Colegio instance");

		try {
			getSession().delete(instance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/**
	 * 
	 * @param Instance
	 *            Colegio Instance to update
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void update(Colegio instance) {
		log.debug("updating Colegio instance");

		try {
			getSession().merge(instance);
			// getSession().update(instance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}

	public Colegio findById(Integer id) {
		log.debug("finding Colegio instance with id: " + id);

		try {
			Colegio instance = (Colegio) getSession().get(Colegio.class, id);

			return instance;
		} catch (RuntimeException re) {
			log.error("finding Colegio failed", re);
			throw re;
		}
	}

	public List<Colegio> findByExample(Colegio instance) {
		log.debug("finding Colegio instance by example");

		try {
			List results = getSession().createCriteria("Colegio")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());

			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/**
	 * Find all Colegio entities with a specific property value.
	 * 
	 * @param value
	 *            the property value to match
	 * @param propertyName
	 *            the property to search in the instance
	 * @return List< Colegio> found by query
	 */
	public List<Colegio> findByProperty(String propertyName, Object value) {
		log.debug("finding Colegio instance with property: " + propertyName
				+ ", value: " + value);

		try {
			String queryString = "from Colegio as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);

			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Colegio> findByColecoddanen(Object colecoddanen) {
		return findByProperty(COLECODDANEN, colecoddanen);
	}

	public List<Colegio> findByColedescripv(Object coledescripv) {
		return findByProperty(COLEDESCRIPV, coledescripv);
	}

	public List<Colegio> findByColedireccionv(Object coledireccionv) {
		return findByProperty(COLEDIRECCIONV, coledireccionv);
	}

	public List<Colegio> findByColeemailv(Object coleemailv) {
		return findByProperty(COLEEMAILV, coleemailv);
	}

	public List<Colegio> findByColefaxv(Object colefaxv) {
		return findByProperty(COLEFAXV, colefaxv);
	}

	public List<Colegio> findByColeidn(Object coleidn) {
		return findByProperty(COLEIDN, coleidn);
	}

	public List<Colegio> findByColelogov(Object colelogov) {
		return findByProperty(COLELOGOV, colelogov);
	}

	public List<Colegio> findByColesectorv(Object colesectorv) {
		return findByProperty(COLESECTORV, colesectorv);
	}

	public List<Colegio> findByColetelefonofv(Object coletelefonofv) {
		return findByProperty(COLETELEFONOFV, coletelefonofv);
	}

	public List<Colegio> findByColezonav(Object colezonav) {
		return findByProperty(COLEZONAV, colezonav);
	}

	public List<Colegio> findByLugaidn(Object lugaidn) {
		return findByProperty(LUGAIDN, lugaidn);
	}

	/**
	 * Find all Colegio entities.
	 * 
	 * @return List<Colegio> all Colegio instances
	 */
	public List<Colegio> findAll() {
		log.debug("finding all Colegio instances");

		try {
			String queryString = "from Colegio";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List<Colegio> findByCriteria(String whereCondition) {
		log.debug("finding Colegio " + whereCondition);

		try {
			String where = ((whereCondition == null) || (whereCondition
					.length() == 0)) ? "" : ("where " + whereCondition);
			final String queryString = "select model from Colegio model "
					+ where;
			Query query = getSession().createQuery(queryString);
			List<Colegio> entitiesList = query.list();

			return entitiesList;
		} catch (RuntimeException re) {
			log.error("find By Criteria in Colegio failed", re);
			throw re;
		}
	}

	public List<Colegio> findPageColegio(String sortColumnName,
			boolean sortAscending, int startRow, int maxResults) {
		log.debug("finding Colegio findPageColegio");

		if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
			try {
				String queryString = "select model from Colegio model order by model."
						+ sortColumnName
						+ " "
						+ (sortAscending ? "asc" : "desc");

				return getSession().createQuery(queryString)
						.setFirstResult(startRow).setMaxResults(maxResults)
						.list();
			} catch (RuntimeException re) {
				throw re;
			}
		} else {
			try {
				String queryString = "select model from Colegio model";

				return getSession().createQuery(queryString)
						.setFirstResult(startRow).setMaxResults(maxResults)
						.list();
			} catch (RuntimeException re) {
				throw re;
			}
		}
	}

	public Long findTotalNumberColegio() {
		log.debug("finding Colegio count");

		try {
			String queryString = "select count(*) from Colegio model";
			Query queryObject = getSession().createQuery(queryString);

			return (Long) queryObject.list().get(0);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List<Colegio> encontrarColegios(int startingAt, int maxPerPage,
			String order, String sortField, Map<String, String> filters,
			String lugaidn) {
		System.out.println("filters.get('colecoddanen'): "
				+ filters.get("colecoddanen")
				+ " filters.get('coledescripv'): "
				+ filters.get("coledescripv"));

		String colecoddanen = filters.get("colecoddanen");
		String coledescripv = filters.get("coledescripv");

		if (colecoddanen == null) {
			colecoddanen = "";
		}

		if (coledescripv == null) {
			coledescripv = "";
		}

		if (lugaidn == null) {
			lugaidn = "";
		}

		String queryString = "select c from Colegio c where coledescripv like '%"
				+ coledescripv.toUpperCase()
				+ "%' and cast(colecoddanen as string) like '%"
				+ colecoddanen
				+ "%' and cast(lugaidn as string) like '%"
				+ lugaidn
				+ "%' order by " + sortField + " " + order;

		Query queryObject = getSession().createQuery(queryString);
		queryObject.setFirstResult(startingAt);
		queryObject.setMaxResults(maxPerPage);
		return queryObject.list();
	}

	public Long findTotalNumberColegio(Map<String, String> filters, String lugaidn) {
		String colecoddanen = filters.get("colecoddanen");
		String coledescripv = filters.get("coledescripv");

		if (colecoddanen == null) {
			colecoddanen = "";
		}

		if (coledescripv == null) {
			coledescripv = "";
		}
		
		if (lugaidn == null) {
			lugaidn = "";
		}

		try {
			String queryString = "select count(*) from Colegio where coledescripv like '%"
					+ coledescripv.toUpperCase()
					+ "%' and cast(colecoddanen as string) like '%"
					+ colecoddanen 
					+ "%' and cast(lugaidn as string) like '%"
					+ lugaidn
					+ "%'";
			Query queryObject = getSession().createQuery(queryString);
			return (Long) queryObject.list().get(0);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List<ColegioDTO> findAllColegioDTO() {
		log.debug("finding all Colegio instances");

		try {
			String queryString = "from Colegio";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
}